package org.codeart.juc.atomic;

import java.util.concurrent.atomic.AtomicStampedReference;

public class AtomicStamp {

    public static void main(String[] args) throws InterruptedException {
        AtomicStampedReference<String> reference = new AtomicStampedReference<>("A", 0);
        Thread t1 = new Thread(() -> {
            int stamp = reference.getStamp();
            boolean succeeded = reference.compareAndSet("A", "B", stamp, stamp + 1);
            System.out.printf("t1: %s\n", succeeded);
        }, "t1");
        Thread t2 = new Thread(() -> {
            int stamp = reference.getStamp();
            boolean succeeded = reference.compareAndSet("B", "A", stamp, stamp + 1);
            System.out.printf("t2: %s\n", succeeded);
        }, "t2");
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println(reference.getReference());
    }

}
